package leetcode_tag.二分查找;

import leetcode.LeetCode;

public class LeetCode69 {

    public static void main(String[] args) {
        LeetCode69 leetCode69=new LeetCode69();
        leetCode69.mySqrt1(2147395599);
    }

    public int mySqrt1(int x) {
        int l = 0, r = x, result = 0;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            // 强转成long
            long tmp = (long)mid * mid;
            if (tmp == x)
                return mid;
            else if (tmp < x) {
                result = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        return result;
    }

    public int mySqrt(int x) {
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long)mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            }
            else {
                r = mid - 1;
            }
        }
        return ans;
    }
}
